// import { delay } from 'redux-saga';
import {
    delay,
    take,
    put,
    fork,
    call,
    takeEvery,
    takeLatest
} from 'redux-saga/effects';
import { actionTypes } from '../actions/saga';
import fetch from '../../services/api';
/**
 *  saga
 */
function* fetchNewsType() {
    try {
        // yield delay(9000);
        const { data } = yield call(fetch.newsType);
        yield put({ type: actionTypes.TYPE_SUCCESS, payload: data });
        //获取列表
        // yield call(fetchNewsList, { id: data[0].typeId });
    } catch (error) {
        yield put({ type: actionTypes.TYPE_ERROR, error });
    }
}
function* fetchNewsList(action) {
    console.log('would了');
    console.log(action);
    try {
        const { data } = yield call(fetch.newsList, action.id, action.page);
        yield put({ type: actionTypes.LIST_SUCCESS, payload: data });
    } catch (error) {
        yield put({ type: actionTypes.LIST_ERROR, error });
    }
}
function* fetchNewsDetail(action) {
    console.log('新闻详情了');
    console.log(action);
    try {
        const { data } = yield call(fetch.newsDetail, action.id);
        yield put({ type: actionTypes.DETAIL_SUCCESS, payload: data });
    } catch (error) {
        yield put({ type: actionTypes.DETAIL_ERROR, error });
    }
}

function* watchNewsType() {
    yield takeLatest(actionTypes.TYPE_REQUEST, fetchNewsType);
}
function* watchNewsList() {
    yield takeEvery(actionTypes.LIST_REQUEST, fetchNewsList);
}
function* watchNewsDetail() {
    yield takeEvery(actionTypes.DETAIL_REQUEST, fetchNewsDetail);
}
/* function* watchFetchTypes() {
    while (true) {
        yield take(actionTypes.TYPEREQUEST);
        yield fork(fetchTypes);
    }
}
 */

export default [
    fork(watchNewsType),
    fork(watchNewsList),
    fork(watchNewsDetail)
];
